package com.hengtongxing.hejiayun.shop.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.bean.OrderListBean;
import com.hengtongxing.hejiayun.homepage.ApplyForRefundActivity;
import com.hengtongxing.hejiayun.homepage.RefundRecordActivity;
import com.hengtongxing.hejiayun.utils.GlideUtils;
import com.hengtongxing.hejiayun.utils.NumberFormatUtils;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class OrderProductListAdapter extends RecyclerView.Adapter<OrderProductListAdapter.ViewHolder> {



    private final Activity activity;
    private final int order_status;

    private List<OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public OrderProductListAdapter(Activity activity,List<OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean> listsBeans,int order_status) {
        this.activity = activity;
        this.listsBeans = listsBeans;
        this.order_status = order_status;
    }

    public void setOnCallBackListener(OrderProductListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_order_product_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean dataBean = listsBeans.get(position);
        GlideUtils.loadImage(context, holder.ivGoodsThumb, dataBean.getShop_goods_thumb());
        holder.tvGoodsName.setText(dataBean.getShop_goods_name());
        String money = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getShop_goods_price()) / 100);
        holder.tvGoodsPrice.setText(money);
        holder.tvGoodsNorm.setText(dataBean.getNorm_name());
        holder.tvGoodsNum.setText("x" + dataBean.getCount() + "");

        if (order_status==0||order_status==1||order_status==9){
            holder.tv_after_sale.setVisibility(View.GONE);
        }else {
            holder.tv_after_sale.setVisibility(View.VISIBLE);
        }


        holder.tv_after_sale.setOnClickListener(v -> {
            int return_goods_status = dataBean.getReturn_goods_status();
            if (return_goods_status==1){
                //退款
                showPictureSelectorDialog(dataBean);
            }else {
                //退款记录
                Intent intent = new Intent(context, RefundRecordActivity.class);
                intent.putExtra("id",dataBean.getId());
                context.startActivity(intent);
            }
        });

    }
    private void showPictureSelectorDialog(OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean dataBean) {

        BottomSheetDialog selectPicDialog = new BottomSheetDialog(context, R.style.Dialog);
        selectPicDialog.getWindow().getAttributes().windowAnimations = R.style.commomDialog;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_cargo_status, null);
        //退款
        TextView tv_refund = view.findViewById(R.id.tv_refund);
        //退货退款
        TextView tv_return_refund = view.findViewById(R.id.tv_return_refund);
        //取消
        TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);

        tv_refund.setOnClickListener(v -> {
            Intent intent = new Intent(context, ApplyForRefundActivity.class);
            intent.putExtra("type","0");//退款
            intent.putExtra("shop_goods_thumb",dataBean.getShop_goods_thumb());
            intent.putExtra("shop_goods_name",dataBean.getShop_goods_name());
            intent.putExtra("norm_name",dataBean.getNorm_name());
            intent.putExtra("shop_goods_price",NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getShop_goods_price()) / 100));
            intent.putExtra("count","x" + dataBean.getCount() + "");
            intent.putExtra("order_status",order_status);
            intent.putExtra("order_goods_id", dataBean.getShop_goods_id());
            intent.putExtra("id", dataBean.getId());
            context.startActivity(intent);
            selectPicDialog.dismiss();
            activity.finish();
        });
        tv_return_refund.setOnClickListener(v -> {

            Intent intent = new Intent(context, ApplyForRefundActivity.class);
            intent.putExtra("type","1");//退款退货
            intent.putExtra("shop_goods_thumb",dataBean.getShop_goods_thumb());
            intent.putExtra("shop_goods_name",dataBean.getShop_goods_name());
            intent.putExtra("norm_name",dataBean.getNorm_name());
            intent.putExtra("shop_goods_price",NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getShop_goods_price()) / 100));
            intent.putExtra("count","x" + dataBean.getCount() + "");
            intent.putExtra("order_status",order_status);
            intent.putExtra("order_goods_id", dataBean.getShop_goods_id());
            intent.putExtra("id", dataBean.getId());
            context.startActivity(intent);
            selectPicDialog.dismiss();
            activity.finish();
        });
        cancel.setOnClickListener(v -> {
            selectPicDialog.dismiss();
        });

        selectPicDialog.setContentView(view);
        selectPicDialog.show();
    }
    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_goods_thumb)
        RoundedImageView ivGoodsThumb;
        @BindView(R.id.tv_goods_name)
        TextView tvGoodsName;
        @BindView(R.id.tv_goods_norm)
        TextView tvGoodsNorm;
        @BindView(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @BindView(R.id.tv_goods_num)
        TextView tvGoodsNum;
        @BindView(R.id.tv_after_sale)
        TextView tv_after_sale;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
